"use strict";

var url_get_token = '/test/face/token?uid=%uid';
function getToken(uid, cb) {
  var url = url_get_token.replace('%uid', uid);
  $.get(url, cb);
}


var url_upload_user = '/api/v1/face/user/upload';
function uploadUser(token, faceFile, cb) {
  var formData = new FormData();
  formData.append("face", faceFile);
  formData.append("token", token);
  $.ajax({
    url: url_upload_user,
    type: "POST",
    processData: false,
    contentType: false,
    data: formData,
    success: function (data) {
      cb(null, data);
    }, error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}

var url_parts = '/api/v1/face/parts/all?token=%token';
function getFaceParts(token, cb) {
  var url = url_parts.replace('%token', token);
  $.get(url, cb);
}


var url_render = '/api/v1/face/render';
function render(json, cb) {
  $.ajax({
    url: url_render,
    type: "POST",
    headers: {"Content-Type": "application/json;charset=UTF-8"},
    data: JSON.stringify(json),
    success: function (res) {
      cb(null, res);
    },
    error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}


var url_upload_star = '/manapi/v1/face/star/upload';
function uploadStar(token, face, name, cb) {
  var formData = new FormData();
  formData.append("token", token);
  formData.append("face", face);
  formData.append("name", name);
  $.ajax({
    url: url_upload_star,
    type: "POST",
    processData: false,
    contentType: false,
    data: formData,
    success: function (data) {
      cb(null, data);
    }, error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}

var url_star_list = '/manapi/v1/face/star/list?token=%token';
function getStarList(token, cb) {
  var url = url_star_list.replace('%token', token);
  $.ajax({
    url: url,
    type: "GET",
    success: function (res) {
      cb(null, res);
    },
    error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}

var url_star_parts = '/manapi/v1/face/parts?starid=%starid&&token=%token';
function getStarPart(token, starid, cb) {
  $.ajax({
    url: url_star_parts.replace('%starid', starid).replace('%token', token),
    type: "GET",
    success: function (res) {
      cb(null, res);
    },
    error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}

// update star's name
function updateStarName(token, starid, name, cb) {
  $.ajax({
    url: '/manapi/v1/face/star/edit',
    type: "POST",
    headers: {"Content-Type": "application/json;charset=UTF-8"},
    data: JSON.stringify({token: token, starid: starid, name: name}),
    success: function (res) {
      cb(null, res);
    },
    error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}

var url_parts_upload = '/manapi/v1/face/parts/upload';
// browdesc, browfile, // 眉毛
//  eyedesc, eyefile, // 眼睛
//  nosedesc, nosefile, // 鼻子
//  mousedesc, mousefile, // 嘴巴
function updateBrow(token, starid, json, cb) {
  var formData = new FormData();
  formData.append("token", token);
  formData.append("starid", starid);
  json.browdesc && formData.append("browdesc", json.browdesc);
  json.browdesc && formData.append("browfile", json.browfile);
  //
  json.eyedesc && formData.append("eyedesc", json.eyedesc);
  json.eyefile && formData.append("eyefile", json.eyefile);
  //
  json.nosedesc && formData.append("nosedesc", json.nosedesc);
  json.nosefile && formData.append("nosefile", json.nosefile);
  //
  json.mousedesc && formData.append("mousedesc", json.mousedesc);
  json.mousefile && formData.append("mousefile", json.mousefile);
  $.ajax({
    url: url_parts_upload,
    type: "POST",
    processData: false,
    contentType: false,
    data: formData,
    success: function (data) {
      cb(null, data);
    }, error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}


function updatePartsOrder(token, orders, cb) {
  $.ajax({
    url: '/manapi/v1/face/parts/order',
    type: "POST",
    headers: {"Content-Type": "application/json;charset=UTF-8"},
    data: JSON.stringify({token: token, orders: orders}),
    success: function (res) {
      cb(null, res);
    },
    error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}

function postBase64Img(str, cb) {
  $.ajax({
    url: '/test/v1/base64test',
    type: "POST",
    headers: {"Content-Type": "application/json;charset=UTF-8"},
    data: JSON.stringify({base64: str}),
    success: function (res) {
      cb(null, res);
    },
    error: function (xhr) {
      cb(xhr.responseText);
    }
  });
}